package com.sw.code4j.reverse;

import com.sw.code4j.vo.ConfigVO;
import com.gwssi.fusion.datamodel.bo.TDmColumnBO;
import com.gwssi.fusion.datamodel.bo.TDmDatasourceBO;
import com.gwssi.fusion.datamodel.bo.TDmTableBO;
import com.gwssi.fusion.datamodel.datasource.config.impl.*;
import com.gwssi.fusion.datamodel.datasource.handler.IDataSourceDb;
import com.gwssi.fusion.datamodel.datasource.handler.impl.*;
import com.gwssi.optimus.core.persistence.dao.DAOManager;
import org.apache.commons.collections.map.HashedMap;
import sun.misc.BASE64Decoder;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author: 张上亚 2021/11/30 17:56
 * @desc: 逆向查询工具类
 */
public class Reverse {

    private IDataSourceDb instance;
    private DataSourceConfigDb dsConfig;

    public Reverse(ConfigVO config) throws Exception {
        Map<String, String> props = getConfigMap(config);
        String dbType = config.getDbType();
        getDataSourceImpl(dbType);
        dsConfig.parseBasicProp(props);
        dsConfig.parseCommonProp(props);
        dsConfig.parseSpecificProp(props);
        initDataSource(dsConfig);
    }

    public List<TDmTableBO> reverseTable() throws Exception {
        return instance.reverseTable(dsConfig);
    }

    public List<TDmColumnBO> reverseColumn(String tableName) throws Exception {
        return instance.reverseColumn(dsConfig, tableName);
    }


    private static Map<String, String> getConfigMap(ConfigVO config) {
        Map<String, String> props = new HashedMap();
        props.put("dsType", "db");
        props.put("implType", config.getDbType());
        props.put("dsName", "code4j");
        props.put("connType", "normal");
        props.put("host", config.getHost());
        props.put("port", config.getPort());
        props.put("dbName", config.getDbName());
        props.put("username", config.getUsername());
        props.put("password", config.getPassword());
        return props;
    }


    /**
     * 初始化数据源到连接池
     *
     * @param dsConfig
     */
    public static void initDataSource(DataSourceConfigDb dsConfig) throws Exception {

        TDmDatasourceBO datasourceBO = dsConfig.getTDmDatasourceBO();
        String dsName = datasourceBO.getDsName();

        Properties prop = new Properties();
        prop.put("driverClassName", dsConfig.getDriverClassName());
        prop.put("url", dsConfig.getUrl());
        prop.put("username", dsConfig.getUsername());
        BASE64Decoder base64 = new BASE64Decoder();
        String password = dsConfig.getPassword();
        prop.put("password", password);
        DAOManager daoManager = new DAOManager();
        daoManager.addDataSource(dsName, dsConfig.getClazz(), dsConfig.getImplType(), prop);
        DAOManager.setDefaultDataSourceKey("defaultDataSource");
    }


    public void getDataSourceImpl(String dbType) {
        switch (dbType) {
            case "dm": {
                this.dsConfig = new DataSourceConfigDbDmImpl();
                this.instance = new DataSourceDbDmImpl();
                break;
            }
            case "mysql": {
                this.dsConfig = new DataSourceConfigDbMysqlImpl();
                this.instance = new DataSourceDbMysqlImpl();
                break;
            }
            case "oracle": {
                this.dsConfig = new DataSourceConfigDbOracleImpl();
                this.instance = new DataSourceDbOracleImpl();
                break;
            }
            case "hive": {
                this.dsConfig = new DataSourceConfigDbHiveImpl();
                this.instance = new DataSourceDbHiveImpl();
                break;
            }
            case "kingbase": {
                this.dsConfig = new DataSourceConfigDbKingbaseImpl();
                this.instance = new DataSourceDbKingbaseImpl();
                break;
            }
            default: {
                throw new IllegalArgumentException("未知的数据库类型：" + dbType);
            }
        }

    }
}
